import { defineStore } from "pinia";
import { Names } from "../../store-namespace";
import { _get, _post, _put, _delete, http } from "@/utils/axios";
import { BusinessMInfo, EquipMCarbon } from "@/api/backend/index";
import { useRegion } from "@/store/backend/SysM/region";
import { getSum, getSumCarbon, completeDevices } from "@/utils/common";
import qs from "qs";

export const useBigBoardA = defineStore(Names.BigBoardA, {
  state: () => {
    return {
      devSum: 0, //设备总量
      point: 0,
      users: 0,
      warnDevices: 0, //告警设备
      workDevices: 0, //正常设备
      stopDevices: 0, //停机设备
    };
  },
  getters: {},
  actions: {
    //活性炭监控数据全部获取 只过滤区域region
    getCarbonData(filter: number) {
      return new Promise((resolve, reject) => {
        const region = useRegion();
        //   获取最底层区域id
        region.getLastAreaId(region.originData, filter).then((res: any) => {
          _get(
            EquipMCarbon + "/listByregions",
            {
              regions: [filter, ...res] + "",
            },
            "",
            ""
          )
            .then((res: any) => {
              resolve(res.rows);
            })
            .catch((err) => {
              console.log(err);
            });
        });
      });
    },
    //设备区域分布数据 默认常州不变
    getPoints(filter: number) {
      return new Promise((resolve, reject) => {
        _get(
          "/system/warning/pointstatistics",
          { region: [filter] + "" },
          "",
          ""
        ).then(async (res: any) => {
          console.log("设备区域分布数据", res);
          this.point = await getSum(res.rows); //区域设备总量
          let dataNew: { name: any; value: any; id: any }[] = [];
          res.rows.map((item: any) => {
            let data = {
              name: item.area_name,
              value: item.value,
              id: item.area_id,
            };
            dataNew.push(data);
          });
          resolve(dataNew);
        });
      });
    },
    // 地图
    getMapData(filter: number) {
      return new Promise((resolve, reject) => {
        _get(
          "/system/warning/pointstatisticsMap",
          { region: [filter] + "" },
          "",
          ""
        ).then(async (res: any) => {
          console.log("设备区域分布数据", res);
          this.point = await getSum(res.rows); //区域设备总量
          let dataNew: { name: any; value: any; id: any }[] = [];
          res.rows.map((item: any) => {
            let data = {
              name: item.area_name,
              value: item.value,
              id: item.area_id,
            };
            dataNew.push(data);
          });
          resolve(dataNew);
        });
      });
    },
    //大屏设备统计
    getEquipments(filter: number) {
      return new Promise((resolve, reject) => {
        _get(
          "/system/warning/equipmentstatistics",
          { region: [filter] + "" },
          "",
          ""
        ).then(async (res: any) => {
          console.log("大屏设备统计", res);
          if (res.rows.length > 0) {
            this.devSum = await getSum(res.rows); //设备总量
            let result = completeDevices(res.rows); //填充完整统计
            resolve(result);
          } else {
            this.devSum = 0;
            resolve([
              { value: 0, estate: 0 },
              { value: 0, estate: 1 },
              { value: 0, estate: 2 },
            ]);
          }
        });
      });
    },
    //用户统计数据
    // getUsers(filter: number) {
    //   return new Promise((resolve, reject) => {
    //     _get(
    //       "/system/warning/userstatistics",
    //       { region: [filter] + "" },
    //       "",
    //       ""
    //     ).then(async (res: any) => {
    //       let addr: string[] = [];
    //       let value: number[] = [];
    //       this.users = await getSum(res.rows); //用户总量
    //       res.rows.map((item: any) => {
    //         addr.push(item.area_name);
    //         value.push(item.value);
    //       });
    //       resolve([addr, value]);
    //     });
    //   });
    // },
    getMonthCarbon(filter: number, month: string) {
      return new Promise((resolve, reject) => {
        _get(
          "/system/estate/sumByregion",
          { date: month, region: [filter] + "" },
          "",
          ""
        ).then(async (res: any) => {
          console.log("res", res);
          let addr: string[] = [];
          let value: number[] = [];
          this.users = await getSumCarbon(res.data); //用户总量
          // console.log("活性炭月量");

          // res.rows.map((item: any) => {
          //   addr.push(item.area_name);
          //   value.push(item.value);
          // });
          for (let i = 0; i < res.data.length; i++) {
            addr.push(res.data[i].area_name);
            value.push(res.data[i].sum);
          }
          resolve([addr, value]);
        });
      });
    },
    //初始化region不传值
    getMonthCarbonTwo(time: string) {
      console.log("还原？", time);

      return new Promise((resolve, reject) => {
        _get("/system/estate/sumByregions", { date: time }, "json", "").then(
          async (res: any) => {
            console.log("res", res.data);
            // resolve(res.data);
            let addr: string[] = [];
            let value: number[] = [];
            this.users = await getSumCarbon(res.data); //用户总量
            // console.log("活性炭月量");
            // res.data((item: any) => {
            //   console.log("item", item);
            // });
            for (let i = 0; i < res.data.length; i++) {
              addr.push(res.data[i].area_name);
              value.push(res.data[i].sum);
            }
            console.log("item", [addr, value]);

            resolve([addr, value]);
          }
        );
      });
    },
    //大屏异常设备监控 过滤未处理状态
    getAbnormal(filter: number) {
      return new Promise((resolve, reject) => {
        _get(
          "/system/warning/abnormallist",
          { region: [filter] + "", pageSize: 10000, state: 0 },
          "",
          ""
        ).then((res: any) => {
          resolve(res.rows);
        });
      });
    },

    //过滤筛选本月新增设备数量
    getMonthDevices(filter: any) {
      return new Promise((resolve, reject) => {
        _get(
          "/system/equipment/listBytree?" +
            qs.stringify(filter, { arrayFormat: "indices" }),
          {},
          "json",
          ""
        )
          .then((res) => {
            resolve(res);
          })
          .catch((err) => {
            console.log(err);
            reject(err);
          });
      });
    },
    //获取企业数量
    // getBusiness(filter: number | string) {
    //   return new Promise((resolve, reject) => {
    //     if (filter) {
    //       _get(
    //         "/system/company/list",
    //         { regions: [filter] + "" },
    //         "json",
    //         ""
    //       ).then((res: any) => {
    //         console.log("企业数量", res);
    //       });
    //     }
    //   });
    // },
  },
});
